#!/bin/bash

usage() {
        echo "Usage:"
        echo "  ./build_initrd.sh [-k]"
        echo "  -k: keep rootfs in ram and not swtich to block device"
}

while getopts ":hk" opt
do
        case $opt in
        h)
                O_HELP=y
                ;;
        k)
                O_KEEPINRAM=y
                ;;
        ?)
                echo "there is unrecognized parameter."
                usage
                exit 1
                ;;
    esac
done

if [ "$O_HELP" = "y" ]; then
	usage
	exit 0
fi

source ./envsetup

WS_BB=$WS/test-busybox

#set -v

OUT=$WS_BB/output
export OUT
echo "======> OUT=$OUT"

export CROSS_COMPILE=riscv64-buildroot-linux-gnu-
#export CROSS_COMPILE=riscv64-unknown-linux-gnu-
echo "======> CROSS_COMPILE=$CROSS_COMPILE"

PATH_BB=$WS_BB/busyboxsource
# $ git remote -v
# busybox	git://git.busybox.net/busybox (fetch)
# busybox	git://git.busybox.net/busybox (push)
# origin	git@gitee.com:mirrors/busyboxsource.git (fetch)
# origin	git@gitee.com:mirrors/busyboxsource.git (push)

PATH_ROOTFS=$WS_BB/rootfs
PATH_INITRD_IMAGE=$WS_BB/initrd.img

echo "======> Start building ......"

pushd $PATH_BB

echo "======> Cleanup for FULLY building ......"
rm -rf $OUT
mkdir $OUT

echo "======> \"Settings\" -> \"Build Options\" -> eanble \"Build static binary (no shared libs)\"(CONFIG_STATIC)"
read -p "Would you continue? [Y/N]:" choice

case $choice in
Y | y)
	make O=$OUT menuconfig
	;;
*)
	popd
	exit
	;;
esac

make -j$(nproc) O=$OUT

make O=$OUT install

popd

echo "======> Building busybox is done."

echo "======> Starting to make initramfs image ......"

if [ -d $PATH_ROOTFS ]; then
	rm -rf $PATH_ROOTFS
fi
if [ -f $PATH_INITRD_IMAGE ];then
	rm -f $PATH_INITRD_IMAGE
fi

mkdir $PATH_ROOTFS

pushd $PATH_ROOTFS

cp -r $OUT/_install/* .

mkdir mnt proc sys dev etc etc/init.d

# FIXME: when using built-in root-filesystem and CONFIG_INITRAMFS_SOURCE is set,
# need to manually created these devices files, especially /dev/console,
# otherwise kernel will print "Warning: unable to open an initial console."
# during booting up.
# I guess this is because the built-in root-filesystem is populated early and
# can not wait mdev to create the device nodes.
#
# This issue does not happen when I used bootloader to load the initrd.img.
#
# I wonder if these nodes should be create both for upon cases? Or just for builtin case?
sudo mknod -m 666 dev/console c 5 1
sudo mknod -m 666 dev/null c 1 3

if [ "$O_KEEPINRAM" = "y" ]; then

ln -s /bin/busybox init

touch etc/init.d/rcS

chmod +x etc/init.d/rcS

cat >> etc/init.d/rcS << EOF
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
sleep 1
echo "sleep done!"
/sbin/mdev -s
EOF

else

touch init

chmod +x init

cat >> init << EOF
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
sleep 1
echo "sleep done!"
/sbin/mdev -s
sleep 1
echo "slepp 2 done!"
/bin/mount /dev/mmcblk1p2 /mnt
echo "mount sdcard done!"
exec switch_root /mnt /usr/sbin/init
EOF

fi

find . | cpio --create --format='newc' > $PATH_INITRD_IMAGE

echo "======> Initramfs image is done and created at: $PATH_INITRD_IMAGE"
if [ "$O_KEEPINRAM" = "y" ]; then
	echo "======> Initramfs runs in RAM mode."
else
	echo "======> Initramfs will try to mount /dev/mmcblk1p2 as rootfs."
fi

popd

#rm -rf $PATH_ROOTFS
#rm -rf $OUT

